"""
Gunicorn配置文件
用于生产环境部署
"""

import multiprocessing

# 服务器绑定
bind = "127.0.0.1:8000"

# 工作进程数量
workers = multiprocessing.cpu_count() * 2 + 1

# 工作进程类型
worker_class = "sync"

# 每个工作进程的连接数
worker_connections = 1000

# 工作进程处理请求的最大数量
max_requests = 1000
max_requests_jitter = 100

# 超时设置
timeout = 30
keepalive = 2

# 预加载应用
preload_app = True

# 进程名称
proc_name = "educational_system"

# 日志配置
accesslog = "/var/log/educational_system/access.log"
errorlog = "/var/log/educational_system/error.log"
loglevel = "info"

# 用户和组
user = "www-data"
group = "www-data"

# 临时目录
tmp_upload_dir = None

# 最大客户端数
max_requests_jitter = 50

# 工作进程重启
max_requests = 1000

# 守护进程
daemon = False

# PID文件
pidfile = "/var/run/educational_system.pid"

# 绑定Unix套接字
# bind = "unix:/var/run/educational_system.sock"

def when_ready(server):
    """服务器准备就绪时的回调"""
    server.log.info("Educational System is ready to serve requests")

def worker_int(worker):
    """工作进程中断时的回调"""
    worker.log.info("Worker received INT or QUIT signal")

def pre_fork(server, worker):
    """工作进程fork前的回调"""
    server.log.info("Worker spawned (pid: %s)", worker.pid)

def post_fork(server, worker):
    """工作进程fork后的回调"""
    server.log.info("Worker spawned (pid: %s)", worker.pid)

def worker_abort(worker):
    """工作进程异常终止时的回调"""
    worker.log.info("Worker received SIGABRT signal")